// Learn cc.Class:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/class.html
//  - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/class.html
// Learn Attribute:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
//  - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
//  - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/life-cycle-callbacks.html
/**
 * 刮刮卡
 */
let base = require("panelBase")
let viewConfig = {
    btn_back:{
        node:null,
        name:"btn_back",
        eventType:"click",
        cb:"onBtnBackClick"
    },
    ggkView:{
        node:null,
        name:"ggkView",
        active:false,
    },
    ggkView_btn_close:{
        node:null,
        name:"ggkView/btn_close",
        eventType:"click",
        cb:"onBtnViewCloseClick"
    },
    ggk_aera:{
        node:null,
        name:"ggkView/aera"
    },
    ggk_tips:{
        node:null,
        name:"ggkTips",
        active:false,
    },
    ggk_des:{
        node:null,
        name:"ggkView/des",
    },
    //奖励多少元
    ggK_con:{
        node:null,
        name:"ggkView/reward_con"
    },

    btn_rank:{
        node:null,
        name:"ggkSc/view/content/item_0/btn_item_rank",
        eventType:"click",
        cb:"onBtnRankClick",
    },

    btnGGKTips:{
        node:null,
        name:"ggkTips/btn_close",
        eventType:"click",
        cb:"onBtnGGkTipCloseClick"
    },
    ggkTipsBtnConfirm:{
        node:null,
        name:"ggkTips/btnConfirm",
        eventType:"click",
        cb:"onBtnGGkTipCloseClick"
    },

    ggkCountDown:{
        node:null,
        name:"ggkSc/view/content/item_0/next_count_down/cd",
    },
    ggkBtnTips:{
        node:null,
        name:"ggkSc/view/content/item_0/next_count_down/btn_info",
        eventType:"click",
        cb:"onBtnTipsClick"
    },

    ggkRet:{
        node:null,
        name:"ggkView/aera/ret"
    },
    content:{
        node:null,
        name:"ggkSc/view/content"
    }
};
cc.Class({
    extends: base,

    properties: {
        ggkCom:null,
        ggkData:null,
        countDown:null,
        //发放刮刮卡的时间点
        timeList:[],
        maxCount:0,
        curGGK:null,
        ggkRet:null,
    },

    init(){
        this._super(viewConfig);
        this.node.addComponent("myGGK");
        this.node.addComponent("myCountDown")
        this.ggkCom = this.node.getComponent("myGGK");
        this.countDown = this.node.getComponent("myCountDown");
        this.ggkCom.init(viewConfig["ggk_aera"].node,this.onCompleted.bind(this));
        this.countDown.init(viewConfig["ggkCountDown"].node);
        this.timeList = ["00:00","08:00","12:00","16:00","20:00"];
        this.isInited = true;
        this.maxCount = 5*12;
        this.ggkData = {};
        cc.tg.eventCenter.addEvent("onRefreshGGK",(data)=>{
             //this.ggkData = data;
             for(let i = 0 ; i < this.maxCount ;++i)
             {
                let d = data[i];
                if(d)
                {
                    if(this.ggkData[d.provideTime] == null)
                    {
                        this.ggkData[d.provideTime] = [];
                    }
                    this.ggkData[d.provideTime].push(d);;
                }
                else{
                    break;
                }
             }
             cc.log(this.ggkData);
        });

        cc.tg.eventCenter.addEvent("onUseGGk",(data)=>{
            if(this.curGGK)
            {
                this.curGGK.useState = true;
               // viewConfig["ggk_aera"].active = true;
                this.initRet(data);
                this.ggkRet = data;
            }
        })
        let con = viewConfig["content"].node;
        for(let i = 0; i < con.childrenCount;++i)
        {
            con.children[i].on("click",this.onBtnGGKClick,this);
        }
        this.onHide();
    },
    onBtnBackClick(){
        this.onHide();
    },

    onShow(){
        this._super();
        let curIndex = 0;
        let cb = ()=>{
            for(let i = 0 ; i < this.timeList.length;++i)
            {
                let remainSecs = this.getRemainSeconds(this.timeList[i]);
                if(remainSecs > 0)
                {
                    this.countDown.startCountDown(remainSecs,null,cb,1,1)
                    this.curIndex = i;
                    break;
                }
            }
        }
        cb();
    },
    onBtnGGKClick(){
         this.curGGK = this.getOneGGK();
        cc.tg.gameLogic.toUseGGk(this.curGGK.id);
       // viewConfig["ggkView"].node.active = true;
    },


    onBtnGGkTipCloseClick(){
        viewConfig["ggk_tips"].node.active = false;
    },
    
    initGGKTips(){
        let curIndex = this.getLastHour();
        let tips = viewConfig["ggk_tips"].node;
        let tStr = `<color=#8B5632>派发12张</c><color=#DD4F3A>错过12张</color>`
        for(let i = 0; i < tips.childrenCount;++i)
        {
            let child = tips.children[i];
            if(child.name.indexOf("item") > -1)
            {
                if(child.name == "item_"+curIndex)
                {
                    child.getChildByName("img").active = true;
                }
                else
                {
                    child.getChildByName("img").active = false;
                }
            }
        }
    },

    onBtnRankClick(){
        cc.tg.eventCenter.dispatchEvent("showPanel",{panelName:"ggkRank",active:true});
    },

    onBtnViewCloseClick(){
        viewConfig["ggkView"].node.active = false;
        this.ggkCom.reset();
    },        
    onDestroy(){
        for(let key in viewConfig)
        {
            viewConfig[key].node = null;
        }
    },
    onBtnTipsClick(){
        this.initGGKTips();
        viewConfig["ggk_tips"].node.active = true;
        
    },
    getRemainSeconds(time){
      
        let totalSeconds = 0;
        let date = new Date();
        let index = time.indexOf(":");
        let h = parseInt(time.substr(0,index))
        let min = parseInt(time.substr(index+1,1))
        let sec = parseInt(time.substr(index+2,1))
         totalSeconds = ( h -date.getHours() )*3600 + (min - date.getMinutes())*60 +sec - date.getSeconds();
        return  totalSeconds;
    },

    getLastHour(){
        let ret = 0;
        let h = new Date().getHours();
        for(let i = 0 ; i < this.timeList.length;++i)
        {
            let time = this.timeList[i];
            let index = time.indexOf(":");
            let th = parseInt(time.substr(0,index))
            if(th > h)
            {
                ret = i-1;
                break;
            }
        }
        return ret ;
    },

    getOneGGK(){
        let index = this.getLastHour();
        let key = this.timeList[index];
        if(key)
        {
            let list = this.ggkData[key];
            for(let i = 0; i < list.length;++i)
            {
                let ggk = list[i];
                if(ggk && ggk.useState == false)
                {
                    return ggk;
                }
            }
        }
    },

    initRet(data){
        let view = viewConfig["ggkView"].node;
        let ret = viewConfig["ggkRet"].node;
        let parent = ret.getChildByName("con_bg_yellow")
        let prizeState = data.prizeState;
        let goldNumber = data.goldNumber ;
        let silverNumber = data.silverNumber;

        for(let i = 0 ; i < parent.childrenCount;++i)
        {
            let child = parent.children[i];
            if(child)
            {
                let imgName = ""
                if(i == 2)
                {
                    imgName = prizeState == true ? "gift_2":""
                  
                }
                else if(i == 4 )
                {
                    imgName = goldNumber > 0 ? "gift_0" : ""
                   
                }
                else if(i == 8)
                {
                    imgName = silverNumber > 0 ? "gift_1":""
                   
                }


                this.updateInfo(child,this.getGiftByIndex(imgName),cc.Sprite);
            }   
        }


        view.active = true;
    },

    onCompleted(){
        if(!this.ggkRet)
            return ;
        let str = ""
        if(this.ggkRet.prizeState )
        {
            str = "ggk_0"
        }
        else if(this.ggkRet.goldNumber > 0)
        {
            str = "ggK_3"
        }
        else if(this.ggkRet.silverNumber > 0)
        {
            str = "ggk_4"
        }
        if(str == "")
        {
            cc.log("未中奖，不播放广告");
            return ;
        }
        cc.tg.eventCenter.dispatchEvent("toShowAd",{codeId:str,countDown:3});
        cc.tg.eventCenter.dispatchEvent("showPanel",{panelName:"banner",active:true});
        this.ggkRet = null;
    }
});
